package com.example.deliveryapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.deliveryapplication.entity.BearingType
import com.example.deliveryapplication.entity.SelectString
import com.example.deliveryapplication.sql.AppDatabase
import com.example.deliveryapplication.wiget.AddClientDialog
import kotlinx.android.synthetic.main.activity_client_set.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class BearingTypeActivity : AppCompatActivity() , CenterStringListAdapter.CallBack{
    private val clientDao by lazy { AppDatabase.getDatabase(this).bearingTypeDao() }
    private val mList by lazy {ArrayList<SelectString>()  }
    private val mAdapter by lazy { CenterStringListAdapter(this,mList,this) }
    private val mViewModel by lazy { ViewModelProvider(this)[BearingTypeViewModel::class.java] }
    private var selectedPosition=-1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bearing_type)
        mRecycleView.layoutManager= LinearLayoutManager(this)
        mRecycleView.adapter=mAdapter
        mConfirmBTN.setOnClickListener {
            val dialog=AddClientDialog(this)
            dialog.setTitle("类型")
            dialog.setListener(object :AddClientDialog.CallBack{
                override fun onConfirmClick(name: String) {
                    lifecycleScope.launch(Dispatchers.IO) {
                        val uuid = getNewUUid()
                        var client= BearingType(uuid,name)
                        clientDao.insertBearType(client)
                        initData()
                    }
                }

            })
            dialog.show()
        }
        mViewModel.updateResult.observe(this){
            initData()
        }
        initData()
        mViewModel.result.observe(this){list->
            list.forEach {
                val selectString= SelectString()
                selectString.code=it.id
                selectString.name=it.name.handlerNull()
                mList.add(selectString)
            }
            mAdapter.notifyDataSetChanged()
        }
        mViewModel.deleteTypeResult.observe(this){
            when(it){
                -1->{
                    showToast("在轴瓦型号中已经有使用该类型的数据，请先删除型号表中相关数据")
                }
                0->{
                    showToast("删除失败")
                }
                else->{
                    if(selectedPosition!=-1){
                        mAdapter.removeAt(selectedPosition,mList.size)
                        mList.removeAt(selectedPosition)
                    }
                }
            }
        }
    }



    override fun onItemClick(position: Int, data: SelectString) {

    }

    override fun onItemDelete(position: Int, data: SelectString) {
        commonDialog("是否确认删除？"){
            selectedPosition=position
            mViewModel.deleteType(clientDao,data.code)
        }
    }

    override fun onItemEdit(position: Int, data: SelectString) {
        val dialog=AddClientDialog(this)
        dialog.setTitle("类型")
        dialog.setData(data)
        dialog.setEdit(true)
        dialog.setListener(object :AddClientDialog.CallBack{
            override fun onConfirmClick(name: String) {
                val type=BearingType(data.code,name)
                mViewModel.updateType(clientDao  ,type)
            }
        })
        dialog.show()
    }

    private fun getData(){
        mViewModel.getAll(clientDao)

    }
    private fun initData(){
        mList.clear()
        getData()
    }
}